
#include "UI.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include "command/Command.h" // 包含您的命令处理类

#include "Visualizer.h"
#include "ModelBuilder.h"

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);  // 初始化渲染模块
VTK_MODULE_INIT(vtkInteractionStyle);  // 初始化交互模块

MyWindow::MyWindow(QWidget* parent) : QMainWindow(parent)
{
	// 设置窗口标题
	setWindowTitle("命令处理窗口");

	// 指令输入框
	commandInput = new QLineEdit(this);
	commandInput->setPlaceholderText("输入命令...");

	// 输出打印
	logOutput = new QTextEdit(this);
	logOutput->setReadOnly(true);

	// 执行按钮
	executeButton = new QPushButton("执行", this);
	connect(executeButton, &QPushButton::clicked, this, &MyWindow::onExecuteCommand);

	// 布局
	QVBoxLayout* layout = new QVBoxLayout;
	layout->addWidget(new QLabel("命令输入:"));
	layout->addWidget(commandInput);
	layout->addWidget(new QLabel("日志输出:"));
	layout->addWidget(logOutput);
	layout->addWidget(executeButton);

	QWidget* centralWidget = new QWidget(this);
	centralWidget->setLayout(layout);
	setCentralWidget(centralWidget);
}

void MyWindow::onExecuteCommand()
{
	QString commandText = commandInput->text();
	if (commandText.isEmpty())
	{
		QMessageBox::warning(this, " 错误 ", " 请输入命令.");
		return;
	}

	// 解析指令
	if (Command::sample().getUiCommand(commandText.toUtf8().constData()))
	{
		logOutput->append("命令执行成功: " + commandText);
	}
	else
	{
		logOutput->append("命令执行失败: " + commandText);
	}

	// 清空输入框
	commandInput->clear();
	// trapezoid -name=2d_geometry -width=4 -height=2 -theta=30
	// extrude -name=3d_geometry -source=2d_geometry -distance=0.5
	// show
	if (Command::sample().is_show)
	{
		bool show_src = false;
		TopoDS_Shape src_trapezoid;
		vtkSmartPointer<vtkPolyData> src_vtk_trapezoid;
		if (!Command::sample().trapezoid_width - 0.0 < 0.00001 && !Command::sample().trapezoid_height - 0.0 < 0.00001 && !Command::sample().trapezoid_theta - 0.0 < 0.00001)
		{
			src_trapezoid = ModelBuilder::createIsoscelesTrapezoid(Command::sample().trapezoid_width, Command::sample().trapezoid_height, Command::sample().trapezoid_theta);
			ModelBuilder::CheckShapeType(src_trapezoid);
			src_vtk_trapezoid = ModelBuilder::convertToVTK_3(src_trapezoid);
			show_src = true;
		}
		if (!Command::sample().extrude_distance - 0.0 < 0.00001)
		{
			// 拉伸二维梯形为三维实体
			// 指定 Z 轴方向的拉伸向量
			double distance = Command::sample().extrude_distance;
			gp_Vec direction(0, 0, distance);
			TopoDS_Shape extruded_trapezoid = ModelBuilder::extrude(src_trapezoid, direction);
			ModelBuilder::CheckShapeType(extruded_trapezoid);
			vtkSmartPointer<vtkPolyData> extrudedModel = ModelBuilder::convertToVTK_3(extruded_trapezoid);
			show_src = false;
			Visualizer::visualizeModelWithPicking(extrudedModel);
		}
		if (show_src)
		{
			Visualizer::visualizeModelWithPicking(src_vtk_trapezoid);
		}


	}

}